//
// Created by v on 24-10-28.
//

#ifndef JK_START_WIND_PROTOCOL_WRAP_UPDATE_H
#define JK_START_WIND_PROTOCOL_WRAP_UPDATE_H

#include <stdint.h>

enum
{
    WIND_PRO_UP_CMD_NONE = 0x0,
    WIND_PRO_UP_CMD_VERSION = 0x91,
    WIND_PRO_UP_CMD_QUERY_SYSTEM = 0x92,
    WIND_PRO_UP_CMD_QUERY_PHOTO = 0x93,
    WIND_PRO_UP_CMD_QUERY_CONFIG = 0x94,
    WIND_PRO_UP_CMD_SET_CONFIG = 0x95,
    WIND_PRO_UP_CMD_OPEN_USB_STREAM = 0x9E,
    WIND_PRO_UP_CMD_CLOSE_USB_STREAM = 0x9F,
    WIND_PRO_UP_CMD_UPDATE_FIREALARMS = 0xA0,
};

typedef struct
{
    uint64_t timestamp;
    uint32_t seqNum;
    uint32_t cmdType;
    int result;
} WindProUpdateHeader;

typedef struct
{
    WindProUpdateHeader header;
    char version[16];
} WindProUpdateVersion;

typedef struct
{
    WindProUpdateHeader header;
    int cmd; // 0 - door open, 1 - door close
    unsigned int open_count;
    float angle; // 当次拍照角度
} WindProUpdateSystemInfo;

typedef struct
{
    WindProUpdateHeader header;
    int door_open_angle;
    int capture_angle;
    uint32_t door_open_timeout;
} WindProUpdateConfig;


typedef struct
{
    WindProUpdateHeader header;
    uint32_t open_count;
    int angle;
    uint64_t offset; // 读取照片偏移
    uint64_t length; // 读取照片长度

    // result
    uint64_t size;
    uint8_t  data[2048]; // 照片数据
    uint8_t *data_p;
    int data_len;
} WindProUpdateQueryPhoto;

// parse and return header
int wind_protocol_wrap_up_parse_header(char *data, int len, WindProUpdateHeader *header);

int wind_protocol_wrap_up_parse_response_version(char *data, int len, WindProUpdateVersion *version);

int wind_protocol_wrap_up_gen_request_version(char *data, int len);
int wind_protocol_wrap_up_gen_response_version(char *data, int len, WindProUpdateHeader *header, char *version);

int wind_protocol_wrap_up_gen_request_query_system(char *data, int len);
int wind_protocol_wrap_up_gen_response_query_system(char *data, int len, WindProUpdateSystemInfo *info);
int wind_protocol_wrap_up_parse_response_query_system(char *data, int len, WindProUpdateSystemInfo *info);

int wind_protocol_wrap_up_gen_request_query_config(char *data, int len);
int wind_protocol_wrap_up_gen_response_query_config(char *data, int len, WindProUpdateConfig *config);
int wind_protocol_wrap_up_parse_response_query_config(char *data, int len, WindProUpdateConfig *config);

int wind_protocol_wrap_up_gen_request_set_config(char *data, int len, WindProUpdateConfig *config);
int wind_protocol_wrap_up_gen_response_set_config(char *data, int len, WindProUpdateConfig *config, int result);
int wind_protocol_wrap_up_parse_response_set_config(char *data, int len);
int wind_protocol_wrap_up_parse_request_set_config(char *data, int len, WindProUpdateConfig *config);

int wind_protocol_wrap_up_gen_request_usb_stream(char *data, int len, int open);
int wind_protocol_wrap_up_gen_response_usb_stream(char *data, int len, int result, WindProUpdateHeader *header);
int wind_protocol_wrap_up_parse_response_usb_stream(char *data, int len);

int wind_protocol_wrap_up_gen_request_photo(char *data, int len, WindProUpdateQueryPhoto *photo);
int wind_protocol_wrap_up_gen_response_photo(char *data, int len, WindProUpdateQueryPhoto *photo);
int wind_protocol_wrap_up_parse_response_photo(char *data, int len, WindProUpdateQueryPhoto *photo,WindProUpdateSystemInfo *info);
int wind_protocol_wrap_up_parse_request_photo(char *data, int len, WindProUpdateQueryPhoto *photo);

void create_modbus_task();
#endif // JK_START_WIND_PROTOCOL_WRAP_UPDATE_H
